/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.proteu.hibernate.util;




//import br.com.tminterface.facesbeans.shared.ArrayItems;
//import br.com.tminterface.utils.Cpf;
//import br.com.tminterface.utils.ZerosFabrica;
import br.com.proteu.hibernate.beans.AgendaMedicoProteu;
import br.com.proteu.hibernate.beans.AgendaProteu;
import br.com.proteu.hibernate.beans.AgendamentoMedicoProteu;
import br.com.proteu.hibernate.beans.AgendamentoProteu;
import br.com.proteu.hibernate.beans.MedicosProteu;
import br.com.proteu.hibernate.beans.PacienteProteu;
import br.com.proteu.hibernate.beans.UsuariosProteu;
import br.com.proteu.util.RegexUtils;
import br.com.proteu.util.ZerosFabrica;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;






/**
 *
 * @author eros & ricardo mano    
 */
public class FireBirdHelperProteu {



      static protected Logger log = Logger.getLogger("logs4.tmlab");
//  static public    List<MaterialBean>  listMaterialBeans = new ArrayList<MaterialBean>();




       /**
        *
        *
        * @return a new Session if session is null or a session if session is not null
        */

private static  Session session;
private static Transaction tx;

    public static Session getSessionDude(){


        //Session session = null;
        if(session == null){
            return session = HibernateUtilFacesProteu.getSession();
        }else if(session != null && !session.isOpen()){
            session = HibernateUtilFacesProteu.getSession();
            return session;
        }else{
            return session;
        }
    }


    /**
     *
     * @param clazz
     * @param id
     * @return Object
     */
     public static Object getObject(Class clazz,long id){

        //Session
                session = getSessionDude();//sessionFactory.getCurrentSession();
        //Transaction
                tx = session.beginTransaction();

        Object obj = null;
        try{
        obj  =   session.get(clazz, id);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }



      /**
     *
     * @param clazz
     * @param id
     * @return Object
     */
     public static Object getObject(Class clazz,String id){

        //Session
         session = getSessionDude();//sessionFactory.getCurrentSession();
        //Transaction
         tx = session.beginTransaction();

        Object obj = null;
        try{
        obj  =   session.get(clazz, id);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }


     /**
     *
     * @param clazz
     * @param id
     * @return Object
     */
     public static Object getObject(Class clazz,Object idPk){

        //Session
         session = getSessionDude();//sessionFactory.getCurrentSession();
        //Transaction
         tx = session.beginTransaction();

        Object obj = null;
        try{
        obj  =   session.get(clazz,(Serializable) idPk);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }

     
     /**
     *
     * @param Object
     * @param id
     * @return Object
     */
     public static boolean deleteObject(Object obj){

        //Session session ;
        //Transaction tx ;

                if(obj != null){

                    session =getSessionDude();//sessionFactory.getCurrentSession();
                    tx = session.beginTransaction();
                            try{

                                session.delete(obj);
                                tx.commit();
                                
                            return true;
                            }finally{
                                if(session != null){
                                    session.close();
                                }
                            }
                }
        
                    return false;

    }

       /**
     *
     * @param Object
     * @param id
     * @return Object
     */
     public static boolean deleteListObject(List listObj){

//        Session session ;
//        Transaction tx ;

                if(listObj != null){

                    session =getSessionDude();//sessionFactory.getCurrentSession();
                    tx = session.beginTransaction();
                            try{

                                for(Object o : listObj){
                                    session.delete(o);
                                }

                                tx.commit();

                            return true;
                            }finally{
                                if(session != null){
                                    session.close();
                                }
                            }
                }

                    return false;

    }



      /**
     *
     * @param clazz
     * @param id
     * @return List<Object>
     */
     public static List  getListObject(Class clazz){

        //Session
         session = getSessionDude();
        //Transaction
         tx = session.beginTransaction();
        List result ;


        try{
        Criteria criteria = session.createCriteria(clazz);
         result = criteria.list();
        tx.commit();
        return result;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }
//     public static List  getListObject100(Class clazz){
//
//        //Session
//         session = getSessionDude();
//        //Transaction
//         tx = session.beginTransaction();
//        List result ;
//
//
//        try{
//        Criteria criteria = session.createCriteria(clazz);
//        criteria.setMaxResults(100);
//         result = criteria.list();
//        tx.commit();
//        return result;
//        }finally{
//            if(session != null){
//                session.close();
//            }
//        }
//
//
//    }

      /**
     *
     * @param clazz
     * @param id
     * @return List<Object>
     */
     public static List  getListObjectOrderByField(Class clazz,String orderByField,boolean orderBy){

        //Session
         session = getSessionDude();
        //Transaction
         tx = session.beginTransaction();
        List result ;


        try{
        Criteria criteria = session.createCriteria(clazz);
                    if(orderBy){
                        criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }

         result = criteria.list();
        tx.commit();
        return result;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }







      /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKey( Class classBean, String strKey, Object value){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.like(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }

  /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKeyLike( Class classBean, String strKey, Object value){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.like(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }



  /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKeyEq( Class classBean, String strKey, Object value){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.eq(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }



  /**
 *
 * @param classBean
 * @param strKey
 * @param value
   * @param orderByField
   * @param ascOrDesc
 * @return a first Object from a List of obejcts
 */
 static  public List getListObjectByKeyEqOrderBy( Class classBean, String strKey, Object value,String orderByField,boolean  ascOrDesc){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.eq(strKey, value));
            }

                    if(ascOrDesc){
                        criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }

            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result;
            }



        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }


      /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a first Object from a List of obejcts
 */
 static  public Object getObjectByKey( Class classBean, String strKey, Object value,boolean orderBy){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{
            session = getSessionDude();//sessionFactory.getCurrentSession();
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(classBean);

            if(strKey != null){
                criteria.add(Restrictions.like(strKey, value));
            }
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return result.get(0);
            }

            if(orderBy){
                 criteria.addOrder( Order.asc(strKey) );
            }else{
                criteria.addOrder( Order.desc(strKey) );
            }

        }finally{
            if(session != null){
                session.close();
            }
        }


            return null;
  }



      /**
 *
 * @param classBean
 * @param strKey
 * @param value
 * @return a List of obejcts
 */
 static  public java.util.List getListOfObjectByKey( Class classBean, String strKey, Object value){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{
           

            if(strKey != null){
                 session = getSessionDude();//sessionFactory.getCurrentSession();
                 tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);
                criteria.add(Restrictions.like(strKey, value));
                result = criteria.list();
            }


            
            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }


  static  public java.util.List getListOfObjectByKeyEq( Class classBean, String strKey, Object value){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{
            
            if(strKey != null){
                session = getSessionDude();//sessionFactory.getCurrentSession();
                tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);

                criteria.add(Restrictions.eq(strKey, value));
                criteria.add(Restrictions.isNotNull(strKey));
                result = criteria.list();
            }
            

            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }


    static  public java.util.List getListOfObjectByKeyEq( Class classBean, String strKey, Object value,String orderByField,boolean orderBy){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{

            if(strKey != null){
                session = getSessionDude();//sessionFactory.getCurrentSession();
                tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);

                criteria.add(Restrictions.eq(strKey, value));
                criteria.add(Restrictions.isNotNull(strKey));
                    if(orderBy){
                     criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }
                result = criteria.list();
            }


            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }


     static  public java.util.List getListOfObjectOrderByField( Class classBean,String orderByField,boolean orderBy){

//        Session session = null;
//        Transaction tx = null;
        List result = null;
        try{

          
                session = getSessionDude();//sessionFactory.getCurrentSession();
                tx = session.beginTransaction();

                Criteria criteria = session.createCriteria(classBean);

                //criteria.add(Restrictions.eq(strKey, value));
                //criteria.add(Restrictions.isNotNull(strKey));
                    if(orderBy){
                     criteria.addOrder( Order.asc(orderByField) );
                    }else{
                        criteria.addOrder( Order.desc(orderByField) );
                    }
                result = criteria.list();
            


            tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


            return result;
  }



    /**
     *
     * @param obj save a obj
     *
     */
     public static void saveObject(Object obj){
//        Session session = null;
//        Transaction tx = null;
        try{
        session = getSessionDude();

        tx = session.beginTransaction();
        session.save(obj);
        tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


    }
    /**
     *
     * @param save a list of objects
     */
     public static void saveObjectList(List list){

//         Session session = null;
//         Transaction tx = null;
         try{
             session = getSessionDude();
             tx = session.beginTransaction();

             for(Object obj: list){
                 session.save(obj);
             }
             tx.commit();
         }finally{
             if(session != null){
                 session.close();
             }
         }

    }

     /**
     *
     * @param obj save or update a obj
     *
     */
     public static void saveOrUpdateObject(Object obj){
//        Session session = null;
//        Transaction tx = null;
        try{
        session = getSessionDude();
        //session = getSessionDude();
        tx = session.beginTransaction();
        session.saveOrUpdate(obj);
        tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


    }
     /**
     *
     * @param save or update a list of objects
     */
     public static void saveOrUpdateObjectList(List list){

//         Session session = null;
//         Transaction tx = null;
         try{
             session = getSessionDude();
             tx = session.beginTransaction();

             for(Object obj: list){
                 session.saveOrUpdate(obj);
             }
             tx.commit();
         }finally{
             if(session != null){
                 session.close();
             }
         }

    }

     /**
     *
     * @param update a obj
     *
     */
     public static void updateObject(Object obj){
//        Session session = null;
//        Transaction tx = null;
        try{
        session = getSessionDude();
        //session = getSessionDude();
        tx = session.beginTransaction();
        session.update(obj);
        tx.commit();
        }finally{
            if(session != null){
                session.close();
            }
        }


    }

        /**
     *
     * @param update a obj and return the same
     *
     */
     public static Object  updateObjectReturnObject(Object obj){
//        Session session = null;
//        Transaction tx = null;

        try{
        session = getSessionDude();
        //session = getSessionDude();
        tx = session.beginTransaction();

        session.update(obj);
        tx.commit();
        return obj;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }

     /**
     *
     * @param save a list of objects
     */
     public static void updateObjectList(List list){

//         Session session = null;
//         Transaction tx = null;
         try{
             session = getSessionDude();
             tx = session.beginTransaction();

             for(Object obj: list){
                 session.update(obj);
             }
             tx.commit();
         }finally{
             if(session != null){
                 session.close();
             }
         }

    }

   



     /**
 *
 * @param login
 * @param senha
 * @return  first  Usuarios object by querying with login and senha
 */

     public static UsuariosProteu getUsuariosByLoginSenha(String login, String senha){
//          Session session = null;
//          Transaction tx = null;
             try{

                 if(login != null && senha != null && login.trim().length() >1 && senha.trim().length() > 1){
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(UsuariosProteu.class);
                     criteria.add(Restrictions.eq("usuStNome", login.trim()));
                     criteria.add(Restrictions.eq("usuStSenha", senha.trim()));
                     //criteria.setMaxResults(10);

                     List<UsuariosProteu> list = criteria.list();
                     if(list != null && list.size()>0){
                         return list.get(0);

                     }

                 }



                 tx.commit();
             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
                 System.err.println(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;
     }

public  static PacienteProteu getPacienteByRGandNome(String rg , String nome){
//            Session session = null;
//            Transaction tx = null;
             try{

                 if(rg != null  && nome != null ) {
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(PacienteProteu.class);
                     //org.hibernate.Query query = session.createQuery(" from PacienteProteu as pac where pac.pacStCodigo =  '0000000657'");
                     // and pac.pacStRG like '"+rg+"' ");
                     criteria.add(Restrictions.like("pacStNome",RegexUtils.spaceLessAndUpperCase(nome)));
                     String myRG = RegexUtils.rgFormater(rg);
                     if(myRG != null){
                        criteria.add(Restrictions.eq("pacStRG",myRG));
                     }
                     
                     tx.commit();
                     //criteria.setMaxResults(1);
                     List<PacienteProteu> list =  criteria.list();
                     
                     if(list != null && list.size()>0){
                         return list.get(0);
                     }

                 }

                 
             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
                 System.err.println(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;
}


public  static PacienteProteu getPacienteByNome(String nome){
//            Session session = null;
//            Transaction tx = null;
             try{

                 if(nome != null ) {
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(PacienteProteu.class);
                    
                     criteria.add(Restrictions.like("pacStNome",RegexUtils.spaceLessAndUpperCase(nome)));
                                         tx.commit();
                    
                     List<PacienteProteu> list =  criteria.list();

                     if(list != null && list.size()>0){
                         return list.get(0);
                     }

                 }


             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
                 System.err.println(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;
}



public  static PacienteProteu getPacienteByRG(String rg ){
//            Session session = null;
//            Transaction tx = null;
             try{

                 if(rg != null) {
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(PacienteProteu.class);
                     
                     String myRG = RegexUtils.rgFormater(rg);
                     if(myRG != null){
                        criteria.add(Restrictions.eq("pacStRG",myRG));
                     }

                     tx.commit();
                     //criteria.setMaxResults(1);
                     List<PacienteProteu> list =  criteria.list();

                     if(list != null && list.size()>0){
                         return list.get(0);
                     }

                 }


             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
                 System.err.println(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;
}




public  static List<AgendamentoMedicoProteu> getListAgendamentoMedicoProteu(Date daySearch, String medicoCodigo){
//            Session session = null;
//            Transaction tx = null;

             try{

                 if(daySearch != null  &&medicoCodigo != null ) {
                     
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(AgendamentoMedicoProteu.class);
                     criteria.add(Restrictions.eq("medStCodigo",medicoCodigo));
                     //criteria.add(Restrictions.eq("agmDtAtendimento",daySearch));
                     

                     tx.commit();
                     
                     return(List<AgendamentoMedicoProteu>) criteria.list();


                 }

             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
                 System.err.println(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;
}



public  static List<AgendamentoProteu> getListAgendamentoProteu(Date daySearch, MedicosProteu medico){
//            Session session = null;
//            Transaction tx = null;

             try{

                 if(daySearch != null  &&medico != null ) {

                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(AgendamentoProteu.class);
                     criteria.add(Restrictions.eq("medStCodigo",medico));
                       DateFormat formatYear = new SimpleDateFormat("yyyy");
                       DateFormat formatMonth = new SimpleDateFormat("M");
                       DateFormat formatDay = new SimpleDateFormat("dd");

                       Integer intYear = new Integer(formatYear.format(daySearch));
                       Integer intMonth = new Integer(formatMonth.format(daySearch))-1;
                       Integer intDay = new Integer(formatDay.format(daySearch));

                       
                       Calendar cStart = Calendar.getInstance();
//                       cStart.setTime(daySearch);
                       cStart.set(intYear, intMonth, intDay    , 0, 0, 0);
                       Calendar cEnd = Calendar.getInstance();
//                       cEnd.setTime(daySearch);
                       cEnd.set(intYear, intMonth, intDay    , 23,59, 59);
                       //Date d = (Date) format.parse(daySearch.toString());
                     //criteria.add(Restrictions.eq("agmDtAtendimento",format.format(daySearch)));
                     //criteria.add(Expression.eq("agdDtAtendimento",daySearch));
                       criteria.add(Expression.between("agdDtConsulta", cStart.getTime(), cEnd.getTime()));
                       criteria.addOrder( Order.asc("agdStHoraConsulta"));


                     tx.commit();

                     return(List<AgendamentoProteu>) criteria.list();


                 }

             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
                 System.err.println(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;
}

public  static void getListAgendamentoProteu(Date daySearch, MedicosProteu medico,List<AgendamentoProteu> listAgendamentoProteu){
//            Session session = null;
//            Transaction tx = null;

             try{

                 if(daySearch != null  &&medico != null ) {
                 
                     if(listAgendamentoProteu != null){listAgendamentoProteu.clear();}else{listAgendamentoProteu = new ArrayList<AgendamentoProteu>();}

                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(AgendamentoProteu.class);
                     criteria.add(Restrictions.eq("medStCodigo",medico));
                       DateFormat formatYear = new SimpleDateFormat("yyyy");
                       DateFormat formatMonth = new SimpleDateFormat("M");
                       DateFormat formatDay = new SimpleDateFormat("dd");

                       Integer intYear = new Integer(formatYear.format(daySearch));
                       Integer intMonth = new Integer(formatMonth.format(daySearch))-1;
                       Integer intDay = new Integer(formatDay.format(daySearch));


                       Calendar cStart = Calendar.getInstance();
//                       cStart.setTime(daySearch);
                       cStart.set(intYear, intMonth, intDay    , 0, 0, 0);
                       Calendar cEnd = Calendar.getInstance();
//                       cEnd.setTime(daySearch);
                       cEnd.set(intYear, intMonth, intDay    , 23,59, 59);
                       //Date d = (Date) format.parse(daySearch.toString());
                     //criteria.add(Restrictions.eq("agmDtAtendimento",format.format(daySearch)));
                     //criteria.add(Expression.eq("agdDtAtendimento",daySearch));
                       criteria.add(Expression.between("agdDtConsulta", cStart.getTime(), cEnd.getTime()));
                       criteria.addOrder( Order.asc("agdStHoraConsulta"));


                     tx.commit();

                     listAgendamentoProteu = criteria.list();

                     //return


                 }

             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
                 System.err.println(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         //return null;
}




 /**
     *
     * @param clazz
     * @param id
     * @return String
     */
     public static String  getDayOfTheWeekByMedico(Date day, String codigoMedico){

        //Session
         session = getSessionDude();
        //Transaction
         tx = session.beginTransaction();
        String strResult =  "";
         int dayOfTheWeek  = day.getDay();
        

              switch(dayOfTheWeek){
                  case 0:
                      strResult = "MEH_CH_DOMINGO";
                  break;
                  case 1:
                      strResult = "MEH_CH_SEGUNDA";
                  break;
                  case 2:
                      strResult = "MEH_CH_TERCA";
                  break;
                  case 3:
                      strResult = "MEH_CH_QUARTA";
                  break;
                  case 4:
                      strResult = "MEH_CH_QUINTA";
                  break;
                  case 5:
                      strResult = "MEH_CH_SEXTA";
                  break;
                  case 6:
                      strResult = "MEH_CH_SABADO";
                  break;
                  default:
                      return "N";

              }

        try{

            //System.out.println(strResult);

       org.hibernate.Query query = session.createSQLQuery
               ("SELECT "+strResult+"  FROM MEDICOSHORARIOS WHERE "+strResult+"  = 'S' AND MED_ST_CODIGO = '"+codigoMedico+"'");
       List<String> list = query.list();

       if(list != null && list.size()>0){
            return  list.get(0);
       }else{
           return "N";
       }
        
        }finally{
            if(session != null){
                session.close();
            }
        }


    }




       /**
     *
     * @param clazz
     * @param id
     * @return String
     */
     public static String  getPacienteNextValue(){

        //Session
         session = getSessionDude();
        //Transaction
         tx = session.beginTransaction();
        List result ;


        try{
       org.hibernate.Query query = session.createSQLQuery("SELECT NEXT VALUE FOR PPACIENTE FROM RDB$DATABASE");//createQuery("EXECUTE PROCEDURE STATUSAMOSTRAPERFIL_NEW 2010102748588602,001,null");
         result = query.list();
        tx.commit();
            if(result != null){
                 return  ZerosFabrica.makeZerosToLeft(result.get(0).toString(), 10);
            }

        return null;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }


         /**
     *
     * @param clazz
     * @param id
     * @return String
     */
     public static String  getAgendaNextValue(){

        //Session
         session = getSessionDude();
        //Transaction
         tx = session.beginTransaction();
        List result ;


        try{
       org.hibernate.Query query = session.createSQLQuery("SELECT NEXT VALUE FOR PAGENDA FROM RDB$DATABASE");//createQuery("EXECUTE PROCEDURE STATUSAMOSTRAPERFIL_NEW 2010102748588602,001,null");
         result = query.list();
        tx.commit();
            if(result != null){
                 return  ZerosFabrica.makeZerosToLeft(result.get(0).toString(), 5);
            }

        return null;
        }finally{
            if(session != null){
                session.close();
            }
        }


    }


    /**
      *
      * @param ageStNome
      * @return lista de agendados
      */
     public static List<AgendaProteu> getListAgendaProteuByNome(String nome){

         try{
             if(nome != null && nome.trim().length() > 4){
                 session = getSessionDude();
                 tx = session.beginTransaction();
                 Criteria criteria = session.createCriteria(AgendaProteu.class);
                 criteria.add(Restrictions.like("ageStNome", RegexUtils.spaceLessAndUpperCase(nome)+"%"));
                 criteria.addOrder(Order.desc("ageStNome"));
                 tx.commit();

                 return criteria.list();
             }
         }catch(Exception xcp){
             HibernateUtilFacesProteu.log.error(xcp);
         }finally{
             if(session != null){
                     session.close();
                 }
         }
         return null;
     }
      /**
      *
      * @param nome
      * @return lista de pacientes
      */

     public static List<PacienteProteu> getListPacienteProteuByNome(String nome){

             try{

                 if(nome != null &&  nome.trim().length() >4 ){
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(PacienteProteu.class);
                     criteria.add(Restrictions.like("pacStNome", RegexUtils.spaceLessAndUpperCase(nome)+"%"));
                     criteria.addOrder( Order.desc("pacStNome") );
                     tx.commit();
                     //criteria.setMaxResults(200);

                             return criteria.list();

                 }
                 


                 
             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }
         return null;

     }


     public static List<PacienteProteu> getListPacienteProteuByRG(String rg){

             try{

                 if(rg != null &&  rg.trim().length() >4 ){
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(PacienteProteu.class);
                     criteria.add(Restrictions.eq("pacStRG", rg));
                     //criteria.setMaxResults(1);

                     return criteria.list();
                     

                 }
                 tx.commit();
             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;

     }


     public static PacienteProteu getPacienteProteuByInfo(String nome,String rg,Date dateNascimento,String cel){

             try{

                 if(rg != null &&  rg.trim().length() >4 ){
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(PacienteProteu.class);
                     if(nome != null){
                        criteria.add(Restrictions.eq("pacStNome", nome));
                     }
                     if(rg != null){
                        criteria.add(Restrictions.eq("pacStRG", rg));
                     }
                     if(dateNascimento != null){
                        criteria.add(Restrictions.eq("pacDtNascimento", dateNascimento));
                     }

                     if(cel != null){
                        criteria.add(Restrictions.eq("pacStCelular", cel));
                     }
                     List<PacienteProteu> listResult = criteria.list();
                     if(listResult != null && listResult.size()>0){
                         return  listResult.get(0);
                     }

                     return null;

                 }
                 tx.commit();
             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;

     }

     public static List<PacienteProteu> getListPacienteProteuByRange(String startRange,String endRange){

             try{

                 
                     session = getSessionDude();
                     tx = session.beginTransaction();
                     Criteria criteria = session.createCriteria(PacienteProteu.class);
                     criteria.add(Expression.between("pacStCodigo", startRange, endRange));
                     criteria.addOrder( Order.asc("pacStCodigo") );
                     tx.commit();



                     return  criteria.list();
                     
                     

                 
                 
             }catch(Exception xcp){
                 HibernateUtilFacesProteu.log.error(xcp);
             }finally{
                 if(session != null){
                     session.close();
                 }
             }

         return null;

     }








}





